package com.pozu.new_retail.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.dao.model.Comment;
import com.pozu.new_retail.dao.model.Message;
import com.pozu.new_retail.dao.model.Order;
import com.pozu.new_retail.dao.model.Ugc;
import com.pozu.new_retail.service.CommentService;
import com.pozu.new_retail.service.MessageService;
import com.pozu.new_retail.service.OrderService;
import com.pozu.new_retail.service.UgcService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;
import com.pozu.new_retail.utils.Tools;

@Controller
public class CommentController {
	
	@Autowired
	private CommentService commentService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private UgcService ugcService;
	@Autowired
	private MessageService messageService;
	
	@ResponseBody
	@RequestMapping(value = "/api/comments/list", method = RequestMethod.GET)
	public Object get(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		int type = Integer.parseInt(request.getParameter("type"));//默认0UGC内容评论1订单评论
		
		String otherId = request.getParameter("otherId");//type为0则是UGC内容id type为1则是orderId
		
		String content = request.getParameter("content");//评论内容
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("type", type);
		map.put("otherId", otherId);
		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("content", content);
		map.put("userId", userId);
		map.put("appId", appId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = commentService.queryByMap(map);

		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/comments/onMe", method = RequestMethod.GET)
	public Object getCommentsMe(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据
		
		HashMap<String, Object> map = new HashMap<String, Object>();

		map.put("userId", userId);
		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("appId", appId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = commentService.queryCommentMeByMap(map);

		return jr;
	}

	@ResponseBody
	@RequestMapping(value = "/api/comments", method = RequestMethod.POST)
	public Object add(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		JsonResult jr = new JsonResult();
		
		Map<String, Object> map = Tools.getRequestData(request);
		
		int type = Integer.parseInt(map.get("type").toString());//默认0UGC内容评论1订单评论
		
		if (type == 0) {
			//ugc内容评论
			
			Comment requestComment = JSONObject.parseObject(map.get("comment").toString(), Comment.class);
			
			requestComment.setType(0);
			requestComment.setUserId(userId);
			requestComment.setAppId(appId);
			
			commentService.saveComment(requestComment);
			
			Ugc ugc = ugcService.queryInfoByUgcId(Long.parseLong(requestComment.getOtherId()));
			
			Message message = new Message();
			
			message.setUserId(ugc.getUserId());
			message.setType(1);
			message.setOtherId(requestComment.getCommentId()+"");
			
			messageService.saveMessage(message);
			
		}else if (type == 1) {
			//订单评论
			
			String orderId = map.get("orderId").toString();//订单id
			
			Order order = orderService.queryByOrderId(orderId);
			
			if(null==order){
				jr.respCode = CodeConstants.ORDERNOTEXIST_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.ORDERNOTEXIST_MSG);
				return jr;
			}
			
			if (order.getOrderState() != 3 && order.getCommentState() != 1) {
				jr.respCode = CodeConstants.ORDERCOMMENTERROR_MSG;
				jr.respMsg = CodeProperties.getString(CodeConstants.ORDERCOMMENTERROR_MSG);
				return jr;
			}
			
			List<Comment> comments = JSONArray.parseArray(JSON.toJSONString(map.get("comments")),Comment.class);//评论
			
			if(null!=comments){
				for(Comment comment:comments){
					comment.setType(1);
					long time = System.currentTimeMillis();
					comment.setCreatedTime(time);
					comment.setUpdatedTime(time);
					comment.setUserId(userId);
					comment.setAppId(appId);
				}
			}
			
			Order temp = new Order();
			temp.setOrderId(orderId);
			temp.setCommentState(2);
			orderService.updateOrder(temp);
			
			commentService.batchInsert(comments);
		}
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);		
		
		return jr;
	}
	
	@ResponseBody
	@RequestMapping(value = "/api/comments", method = RequestMethod.PUT)
	public Object updateComment(HttpServletRequest request) throws Exception{	
	
		Comment requestComment = Tools.getRequestObject(request, Comment.class);
		
		JsonResult jr = new JsonResult();
		
		requestComment.setState(1);
		
		commentService.updateComment(requestComment);
		
		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		
		return jr;
	}	
	
}
